package com.ys.zero.web;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ys.zero.bean.Dict;
import com.ys.zero.bean.Result;
import com.ys.zero.bean.Uarticle;
import com.ys.zero.bean.User;
import com.ys.zero.biz.BizException;
import com.ys.zero.biz.SearchBiz;
import com.ys.zero.biz.UserBiz;
import com.ys.zero.dao.SearchDao;
import com.ys.zero.dao.UserDao;

/**
 * 与搜索相关的Servlet
 * @author Administrator
 *
 */
@WebServlet("/search/*")
public class SearchServlet extends BaseServlet {
	
	private static final long serialVersionUID = 1L;
	private SearchBiz biz = new SearchBiz();
	private SearchDao dao = new SearchDao();
	/**
	 * 首页的搜索
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void mianSearch(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String keyword = request.getParameter("keyword");
		try {
			List<Uarticle> uarticle = biz.seaech(keyword);
			toJson(response,new Result(1,"搜索成功"));
		} catch (BizException e) {
			toJson(response,new Result(0,e.getMessage()));
		}
		
	}	
	/**
	 * 大家都在搜
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void log(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Uarticle> list = dao.showLog();
		toJson(response, list);
	}
	
	protected void showSearch(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//解码
		String log = URLDecoder.decode(request.getParameter("log"),"UTF-8");
		//获取分页参数
		String page =request.getParameter("page");
		page = page==null ? "1":page;
		int pageIndex=Integer.parseInt(page);
		//获得系列数据
		List<Uarticle> list = dao.showSearch(log,pageIndex);
		toJson(response, list);
	}
	/**
	 * 得到文章数量
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void getCount(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String log = URLDecoder.decode(request.getParameter("log"),"UTF-8");
		Object count = dao.getCount(log);
		toJson(response, count);
	}
	/**
	 * 得到作者名字
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void getName(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String log = URLDecoder.decode(request.getParameter("log"),"UTF-8");
		List<User> count = dao.getName(log);
		toJson(response, count);
	}
}
